﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class InvLotLocLpnList : BasePage
    {
        protected Button btnExport;
        protected Button btnNewSearch;
        protected HtmlForm F5012;
        protected GridView gvInvLotLocLpn;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblInvLotLocLpnList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;

        private void BindDataInvLocLotLpnList(InvLotLocLpnQueryEntity InvLtLocLpnEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(InvLtLocLpnEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetInvLotLocLpnByQueryEntity(InvLtLocLpnEntity);
            base.BindData2GridView(this.gvInvLotLocLpn, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnQuery = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnQuery.IsGetAll = true;
            invLotLocLpnQuery.SortField = "SKU_ID";
            this.hdnActionID.Value = "Y";
            this.BindDataInvLocLotLpnList(invLotLocLpnQuery);
            this.gvInvLotLocLpn.ShowFooter = false;
            invLotLocLpnQuery.IsGetAll = false;
            this.SetInvLocLotLpnQueryEntity(invLotLocLpnQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("InvLotLocLpnQuery.xls", this.gvInvLotLocLpn);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnEntity.CurrentPage = "0";
            this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("InvLotLocLpnQuery.aspx?action={0}&OwnerID={1}", base.UrlEncode(this.hdnActionID.Value), base.UrlEncode(this.hdnOwnerID.Value)));
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            if (int.Parse(invLotLocLpnEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invLotLocLpnEntity.CurrentPage = (int.Parse(invLotLocLpnEntity.CurrentPage) + 1).ToString();
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            if (int.Parse(invLotLocLpnEntity.CurrentPage) > 0)
            {
                invLotLocLpnEntity.CurrentPage = (int.Parse(invLotLocLpnEntity.CurrentPage) - 1).ToString();
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
        }

        private InvLotLocLpnQueryEntity GetInvLotLocLpnQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as InvLotLocLpnQueryEntity);
            }
            return (base.PageQueryEntitySession as InvLotLocLpnQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
                invLotLocLpnEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
        }

        protected void gvInvLotLocLpn_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5012", this.gvInvLotLocLpn);
        }

        protected void gvInvLotLocLpn_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
            }
        }

        protected void gvInvLotLocLpn_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string status = this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["HOLD_STATUS"].ToString();
            if (!string.IsNullOrEmpty(status) && (status.ToUpper() != "HOLD"))
            {
                string selectData = string.Empty;
                selectData = (((((base.UrlReplace(this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["LOT"].ToString()) + "," + base.UrlReplace(this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["LOC"].ToString())) + "," + base.UrlReplace(this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["LPN_ID"].ToString())) + "," + base.UrlReplace(this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["SKU_ID"].ToString())) + "," + base.UrlReplace(this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["DESCR"].ToString())) + "," + base.UrlReplace(this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["AVAILABLE_QTY"].ToString())) + "," + base.UrlReplace(this.gvInvLotLocLpn.DataKeys[e.NewSelectedIndex]["PACK_ID"].ToString());
                if (!string.IsNullOrEmpty(selectData))
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + selectData + "';  window.close();</script>", false);
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5250_008");
            }
        }

        protected void gvInvLotLocLpn_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
                invLotLocLpnEntity.SortField = e.SortExpression;
                if (invLotLocLpnEntity.SortDirection == "ASC")
                {
                    invLotLocLpnEntity.SortDirection = "DESC";
                }
                else if (invLotLocLpnEntity.SortDirection == "DESC")
                {
                    invLotLocLpnEntity.SortDirection = "ASC";
                }
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ownerID"]))
            {
                this.hdnOwnerID.Value = base.Request.QueryString["ownerID"].ToString();
            }
            InvLotLocLpnQueryEntity InvLtLocLpnEntity = new InvLotLocLpnQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    InvLtLocLpnEntity.Loc = this.PreviousPage.Loc;
                    InvLtLocLpnEntity.Lot = this.PreviousPage.Lot;
                    InvLtLocLpnEntity.SkuID = this.PreviousPage.SkuID;
                    InvLtLocLpnEntity.OwnerID = this.PreviousPage.OwnerID;
                    InvLtLocLpnEntity.LpnID = this.PreviousPage.Lpn;
                    InvLtLocLpnEntity.ExternalLot = this.PreviousPage.ExternalLot;
                    InvLtLocLpnEntity.VendorID = this.PreviousPage.VendorID;
                    InvLtLocLpnEntity.SkuDescr = this.PreviousPage.SkuDescr;
                    if (this.PreviousPage.History == "N")
                    {
                        InvLtLocLpnEntity.Qty = " > 0";
                    }
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
                InvLtLocLpnEntity.OwnerID = this.PreviousPage.OwnerID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"];
            }
            InvLtLocLpnEntity.IsGetAll = false;
            InvLtLocLpnEntity.CurrentPage = "0";
            InvLtLocLpnEntity.PageSize = this.gvInvLotLocLpn.PageSize.ToString();
            InvLtLocLpnEntity.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvInvLotLocLpn.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetInvLocLotLpnQueryEntity(InvLtLocLpnEntity);
            this.BindDataInvLocLotLpnList(InvLtLocLpnEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
            if (base.Request.QueryString["NewSearch"] == "false")
            {
                this.btnNewSearch.Enabled = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetInvLocLotLpnQueryEntity(InvLotLocLpnQueryEntity InvLtLocLpnEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = InvLtLocLpnEntity;
            }
            else
            {
                base.PageQueryEntitySession = InvLtLocLpnEntity;
            }
        }

        public InvLotLocLpnQuery PreviousPage
        {
            get
            {
                return (InvLotLocLpnQuery) base.PreviousPage;
            }
        }
    }
}

